package cn.mugutu.finance.dto;

import cn.mugutu.finance.domain.system.Menu;
import cn.mugutu.finance.domain.system.Resource;
import cn.mugutu.finance.domain.system.Role;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Data
public class RoleDto {

    private Long id;
    private String name;
    private String description;
    private List<String> resourceNames = new ArrayList<>();
    private List<String> menuNames = new ArrayList<>();

    private List<Long> resourceIds = new ArrayList<>();
    private List<Long> menuIds = new ArrayList<>();


    public RoleDto(Role role) {
        super();
        this.id = role.getId();
        this.name = role.getName();
        this.description = role.getDescription();

        Set<Resource> resources = role.getResources();
        if (resources != null) {
            for (Resource resource : resources) {
                this.resourceNames.add(resource.getName());
                this.resourceIds.add(resource.getId());
            }
        }

        Set<Menu> menus = role.getMenus();
        if (menus != null) {
            for (Menu menu : menus) {
                this.menuNames.add(menu.getName());
                this.menuIds.add(menu.getId());
            }
        }

    }

    /**
     * 将List<Role> 转化为List<RoleDot>
     */
    public static List<RoleDto> roleListToRoleDtoList(List<Role> list) {
        List<RoleDto> dtoList = new ArrayList<>();
        for (Role role : list) {
            RoleDto dto = new RoleDto(role);
            dtoList.add(dto);
        }
        return dtoList;
    }


}
